using System;
using Impact.Controllers;
using Impact.Web.Foundation.Controls;
using Impact.Model.Objects;

namespace Impact.Web.Admin
{
    public partial class EditEvent : Impact.Web.Foundation.PageBase, Impact.Views.IEventEditorView
    {
        private EventController controller;

        /// <summary>
        /// override of onload to perform startup functionality
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );

            controller = new EventController( this, UserId );
            controller.InitializeEditor( !IsPostBack );
        }

        /// <summary>
        /// perform data binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            emailTemplate.DataSource = new EmailTemplateCollection().Load();
            emailTemplate.DataValueField = EmailTemplate.Columns.TemplateID;
            emailTemplate.DataTextField = EmailTemplate.Columns.Name;
            emailTemplate.DataBind();

        }

        /// <summary>
        /// event handler for the submit button
        /// </summary>
        protected void SubmitCommand( object sender, EventArgs e )
        {
            if ( Page.IsValid )
            {
                controller.SaveEvent();
                MessageBox.Show( "Your changes have been saved." );
                Redirect( "~/Admin/Events.aspx" );
            }
        }

        /// <summary>
        /// event handler for the cancel button
        /// </summary>
        protected void CancelCommand( object sender, EventArgs e )
        {
            Redirect( "~/Admin/Events.aspx" );
        }

        #region IEventEditorView Members

        /// <summary>
        /// populate form elements with data from the given event
        /// </summary>
        public void PopulateForm( Event evt )
        {
            ViewState[Event.Columns.EventID] = evt.EventID;

            name.Text = evt.Name;
            startTime.SelectedDate = evt.StartTime;
            endTime.SelectedDate = evt.EndTime;
            location.Text = evt.Location;
            description.Text = evt.Description;

            emailTemplate.SelectedValue = evt.EmailTemplateID.ToString();

        }

        /// <summary>
        /// populate an Event object with data from the form elements
        /// </summary>
        public Event RetrieveFormData()
        {
            Event evt = null;
            if ( ViewState[Event.Columns.EventID] != null )
            {
                int eventId = (int)ViewState[Event.Columns.EventID];
                if ( eventId > 0 )
                {
                    evt = EventController.GetEvent( eventId.ToString() );
                }
            }

            if ( evt == null )
            {
                evt = new Event();
            }

            evt.Name = name.Text;
            evt.Location = location.Text;
            evt.Description = description.Text;

            if ( startTime.SelectedDate.HasValue )
            {
                evt.StartTime = startTime.SelectedDate.Value;
            }
            if ( endTime.SelectedDate.HasValue )
            {
                evt.EndTime = endTime.SelectedDate.Value;
            }

            evt.EmailTemplateID = Int32.Parse( emailTemplate.SelectedValue );

            return evt;
        }

        #endregion
    }
}
